package conclusion;

import java.util.stream.Stream;



/**
 * @author joker
 * @date 2022/4/25 0025 - 0:28
 * 排序
 */
public class Test04 {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.generate(() -> (int) (Math.random() * (1000))).limit(100000);
        Integer[] data = stream.toArray(Integer[]::new);
        //        stream.forEach(System.out::println);
        //        Integer[] data = (Integer[]) stream.toArray();
//        System.out.println(Arrays.toString(data));
        long start= System.currentTimeMillis();
        quickSort(data,0,data.length-1);
//        bufSort(data);
        long end=System.currentTimeMillis();
//        System.out.println(Arrays.toString(data));
        System.out.println(end-start);
    }
    public static void quickSort(Integer[] data, int begin, int end){
        if(begin>end)
            return;
        int pot=data[begin];
        int i=begin;
        int j=end;
        while (i!=j){
            while (data[j]>=pot && j>i)
                j--;
            while (data[i]<=pot && j>i)
                i++;
            if(j>i){
                int tmp=data[j];
                data[j]=data[i];
                data[i]=tmp;
            }
        }
        data[begin]=data[i];
        data[i]=pot;
        quickSort(data,begin,i-1);
        quickSort(data,i+1,end);


    }

    public static void bufSort(Integer[] data){
        for (int i = 0,len=data.length; i < len-1; i++) {
            boolean flag = false;
            for (int j = i; j <len-1-i ; j++) {
                if(data[j]>data[j+1]){
                    int tmp=data[j];
                    data[j]=data[j+1];
                    data[j+1]=tmp;
                    flag=true;
                }
            }
            if(!flag)
                break;
        }
    }
}
